# -*- coding: utf-8 -*-
import scrapy
from day1.items import Day1Item
from scrapy import Request
from scrapy.selector import Selector
class DoubanSpider(scrapy.Spider):
    print('Spider doubanSpider Staring...')
    # 爬虫名 必须静态指定
    # name = urlSettings.SPIDER_NAME
    name = 'douban'
    # 设定域名
    allowed_domains = ['douban.com']
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
    }
    def start_requests(self):
        url = 'https://book.douban.com/tag/互联网'
        yield Request(url, headers=self.headers)
    ##start_urls = ['https://book.douban.com/tag/%E4%BA%92%E8%81%94%E7%BD%91']

    def parse(self, response):
        # sel : 页面源代码
        sel = Selector(response)
        print('获取页面源代码')
        print(sel)

        item = Day1Item()
        books = response.xpath('//ul[@class="subject-list"]/li')
        for book in books:
            item['book_name'] = book.xpath(".//div[@class='info']/h2/a/span").extract()[0]
            item['score'] = book.xpath(".//div[@class='star']/span[$class='rating_nums']").extract()[0]
            item['Introduction'] = book.xpath(".//div[@class='info']/p").extract()[0]
            yield item
